

from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSignal, QTimer, Qt
from PyQt5.QtWidgets import QApplication, QMessageBox


import mysq
from ui.zhu2 import Ui_Frame as tq
from biao_ import zi
from redwin_ import red as rw
import sys
from datetime import time
from PyQt5.QtCore import QThread
from bluewin_ import bwin as bw
import serial
import time


class hujuThread(QThread):
    compare_signal = pyqtSignal(str)
    def __init__(self):
        super().__init__()
        self.ser = serial.Serial()
        self.start_huju=False
        self.ser.port = "COM3"
        self.ser.baudrate = 115200 # 波特率
        self.ser.bytesize = 8
        self.ser.stopbits = 1
        self.ser.parity = "N"  # 奇偶校验位



    def run(self):
        while True:
            if self.start_huju:
                a=('5A 06 FF 06 06 02 01 08 F2 A5','5A 01 08 05 86 02 30 00 B1 A5','5A 06 FF 06 85 02 0B 33 FD A5','5A 01 08 0C 06 02 00 00 01 A5',
                   '5A 01 08 0C 86 02 30 00 B1 A5','5A 01 08 0C 86 02 01 00 80 A5','5A 01 08 0C 86 03 01 00 80 A5',
                   '5A 01 08 0C 86 02 01 00 80 A5','5A 01 08 0D 86 02 01 00 80 A5',)
                b=('5A 01 08 0C 86 02 02 00 80 A5','5A 01 08 0D 86 02 02 00 80 A5')
                c=('5A 01 08 06 86 02 12 00 80 A5')
                # d=(5A 01 08 0C 86 02 01 00 80 A5)
                # self.compare_signal.emit(c)
                for i in a:
                    # time.sleep(1)
                    self.compare_signal.emit(i)
            time.sleep(1)


class LabelsThread(QThread):
    compare_signal = pyqtSignal(str)

    def __init__(self, label1, label2, label3, label4):
        super().__init__()
        self.label1 = label1
        self.label2 = label2
        self.label3 = label3
        self.label4 = label4
        self.start_label = False

    def run(self):
        while True:
            while self.start_label:
                # 加分，扣分
                if self.label1.text() == self.label2.text() :
                    self.compare_signal.emit('1+{}'.format(self.label1.text()))

                elif self.label3.text() == self.label4.text() :
                    self.compare_signal.emit('2+{}'.format(self.label3.text()))

                # 将label清零
                for label in [self.label1, self.label2, self.label3, self.label4]:
                    if label.text() != '0':
                        label.setText('0')
                time.sleep(1)

class Taekwondo1(QtWidgets.QMainWindow, tq):
    def __init__(self):
        super(Taekwondo1, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("主界面")#标题
        self.xuhao = 0
        self.countdown_time = 120  # 局倒计时
        self.countdown_time_2 = 60  # 休息倒计时
        self.blue_num_2= 0  # 蓝方分数
        self.blue_num_2 = 0
        self.red_num_2 = 0  # 红方分数
        self.red_num_2 = 0
        self.blue_warning_num = 0  # 蓝方警告
        self.red_warning_num = 0  # 红方警告
        self.blue_deduction_num = 0  # 蓝方扣分
        self.red_deduction_num = 0  # 红方扣分
        self.round = 1  # 第几局
        self.huju = []  # 串口传输的信息
        self.placeid = ''  # 场地号
        self.red_head_id = ''  # 红方头盔id
        self.red_huju_id = ''  # 红方护具id
        self.blue_head_id = ''  # 蓝方头盔id
        self.blue_huju_id = ''  # 蓝方护具id
        self.blue_huju_dian = ''  # 电量
        self.blue_head_dian = ''
        self.red_huju_dian = ''
        self.red_head_dian = ''
        self.blue_head_lidu = ''
        self.blue_huji_lidu = ''
        self.red_head_lidu = ''
        self.red_huji_lidu = ''
        self.red_bian=''
        self.blue_bian=''
        self.id = ''
        self.dict = {'01': '空闲', '02': '工作', '03': '异常'}
        self.current_row = 0
        self.bisaixuhao = ''
        self.xianchen()#开启线程
        self.InitSlots()  # 连接槽函数


        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_time)
        self.timer_2 = QTimer(self)
        self.timer_2.timeout.connect(self.update_time_2)
    #线程
    def xianchen(self):
        self.compare_thread =LabelsThread(self.label_blue_fs_one_1, self.label_blue_fs_one_2,
                                          self.label_red_fs_one_1, self.label_red_fs_one_2) #比较之后红蓝方加分
        self.compare_thread.compare_signal.connect(self.on_compare_signal)
        self.compare_thread.start()

        self.huju_thread=hujuThread()
        self.huju_thread.compare_signal.connect(self.on_huju_signal)
        self.huju_thread.start()
    #串口传数据，进行分析
    def on_huju_signal(self,data):
        print(data)
        self.huju=data.split()
        print(self.huju)
        self.id=int(self.huju[3][0])%2+1
        self.label_shebei.setText(str(self.id))

        if self.huju[3] == '06' or self.huju[3] == '07':  # 红方护具id显示
            self.red_huju_id = self.huju[3]
            self.label_red_huju_id.setText(str(self.id))
            self.xuhao=3
        elif self.huju[3] == '0A' or self.huju[3] == '0B':  # 红方头盔id
            self.red_head_id = self.huju[3]
            self.label_red_head_id.setText(str(self.id))
            self.xuhao=4
        elif self.huju[3] == '04' or self.huju[3] == '05':  # 蓝方护具id
            self.blue_huju_id = self.huju[3]
            self.label_blue_huju_id.setText(str(self.id))
            self.xuhao = 1
        elif self.huju[3] == '08' or self.huju[3] == '09':  # 蓝方头盔id
            self.blue_head_id = self.huju[3]
            self.label_blue_head_id.setText(str(self.id))
            self.xuhao=2
        elif self.huju[3] == '0C':    # 打分器1
            if self.huju[6][0] !='0': #红方打分
                self.label_red_fs_one_1.setText(self.huju[6][0])
                self.label_dfq_dian_1.setText(self.huju[4][1] + '0')
            elif self.huju[6][1] !='0': #蓝方打分
                self.label_blue_fs_one_1.setText(self.huju[6][1])
                self.label_dfq_dian_2.setText(self.huju[4][1] + '0')

        elif self.huju[3] == '0D': #打分器2
            if self.huju[6][0] !='0': #红方打分
                self.label_red_fs_one_2.setText(self.huju[6][0])
                self.label_dfq_dian_1.setText(self.huju[4][1] + '0')
            elif self.huju[6][1] !='0': #蓝方打分
                self.label_blue_fs_one_2.setText(self.huju[6][1])
                self.label_dfq_dian_2.setText(self.huju[4][1] + '0')

        if self.xuhao ==1: #蓝护具
            self.blue_huju_dian = self.huju[4][1] + '0'
            self.label_blue_huju_dian.setText(self.blue_huju_dian)
            self.label_blue_huju_lidu.setText(self.dict[self.huju[5]])
            if self.huju[4][0] != '0': #不是心跳包
                self.label_blue_huju_fs.setText(str(int(self.huju[6], 16)))
                if self.huju[6] != '00':
                    self.blue_num_2 += 1
                    self.label_blue_csore.setText(str(self.blue_num_2))
                    if self.round == 1:
                        self.label_blue_one_1.setText(str(self.blue_num_2))
                    elif self.round == 2:
                        self.label_blue_two_1.setText(str(self.blue_num_2))
                    elif self.round == 3:
                        self.label_blue_three_1.setText(str(self.blue_num_2))
                    elif self.round == 4:
                        self.label_blue_four_1.setText(str(self.blue_num_2))


        elif self.xuhao ==2: #蓝头盔
            self.blue_head_dian=self.head[4][1]+'0'
            self.label_blue_head_dian.setText(self.blue_head_dian)
            self.label_blue_head_lidu.setText(self.dict[self.huju[5]])
            if self.huju[4][0] != '00': #不是心跳包
                self.label_blue_huju_fs.setText(self.huju[6])
                if self.huju[6] !='00':
                    self.blue_num_2 += 1
                    self.label_blue_csore.setText(str(self.blue_num_2))
                    if self.round == 1:
                        self.label_blue_one_1.setText(str(self.blue_num_2))
                    elif self.round == 2:
                        self.label_blue_two_1.setText(str(self.blue_num_2))
                    elif self.round == 3:
                        self.label_blue_three_1.setText(str(self.blue_num_2))
                    elif self.round == 4:
                        self.label_blue_four_1.setText(str(self.blue_num_2))

        elif self.xuhao ==3: #红护具
            self.red_huju_dian=self.huju[4][1]+'0'
            self.label_red_huju_dian.setText(self.red_huju_dian)
            self.label_red_huju_lidu.setText(self.dict[self.huju[5]])
            if self.huju[4][0] != '0':  # 不是心跳包
                self.label_red_huju_fs.setText(str(int(self.huju[6], 16)))
                if self.huju[6] !='00':
                    self.red_num_2 += 1
                    self.label_red_csore.setText(str(self.red_num_2))
                    if self.round == 1:
                        self.label_red_one_1.setText(str(self.red_num_2))
                    elif self.round == 2:
                        self.label_red_two_1.setText(str(self.red_num_2))
                    elif self.round == 3:
                        self.label_red_three_1.setText(str(self.red_num_2))
                    elif self.round == 4:
                        self.label_red_four_1.setText(str(self.red_num_2))

        elif self.xuhao ==4: #红头盔
            self.red_head_dian=self.head[4][1]+'0'
            self.label_red_head_dian.setText(self.red_head_dian)
            self.label_red_head_lidu.setText(self.dict[self.huju[5]])
            if self.huju[4][0] != '00':  # 不是心跳包
                self.label_red_head_fs.setText(self.huju[6])
                if self.huju[6] !='00':
                    self.red_num_2 += 1
                    self.label_red_csore.setText(str(self.red_num_2))
                    if self.round == 1:
                        self.label_red_one_1.setText(str(self.red_num_2))
                    elif self.round == 2:
                        self.label_red_two_1.setText(str(self.red_num_2))
                    elif self.round == 3:
                        self.label_red_three_1.setText(str(self.red_num_2))
                    elif self.round == 4:
                        self.label_red_four_1.setText(str(self.red_num_2))


    def on_compare_signal(self,data): #加分
        if  data[0]=='1':#蓝方加分
            self.blue_num_2+= int(data[2:])
            self.label_blue_fs.setText(str(self.blue_num_2))
            self.label_blue_csore.setText(str(self.blue_num_2))
            if self.round == 1:
                self.label_blue_one_1.setText(str(self.blue_num_2))
            elif self.round == 2:
                self.label_blue_two_1.setText(str(self.blue_num_2))
            elif self.round == 3:
                self.label_blue_three_1.setText(str(self.blue_num_2))
            elif self.round == 4:
                self.label_blue_four_1.setText(str(self.blue_num_2))

        if data[1]=='2':#红方加分
            self.red_num_2 += int(data[2:])
            self.label_red_fs.setText(str(self.red_num_2))
            self.label_red_csore.setText(str(self.red_num_2))
            if self.round == 1:
                self.label_red_one_1.setText(str(self.red_num_2))
            elif self.round == 2:
                self.label_red_two_1.setText(str(self.red_num_2))
            elif self.round == 3:
                self.label_red_three_1.setText(str(self.red_num_2))
            elif self.round == 4:
                self.label_red_four_1.setText(str(self.red_num_2))

    def InitSlots(self):
        #显示第一组
        self.a=mysq.execute_query_one()
        self.label_6.setText(self.a[0])
        self.label_11.setText(self.a[1])
        self.label_12.setText(self.a[2])
        self.label_14.setText(self.a[3])
        self.label_blue_name.setText(self.a[4])
        self.label_blue_site.setText(self.a[5])
        self.label_red_name.setText(self.a[6])
        self.label_red_site.setText(self.a[7])
        self.bisaixuhao=self.a[8]
        self.blue_bian=self.a[9]
        self.red_bian=self.a[10]



        self.pushButton_start.clicked.connect(self.start_countdown)  # 开始比赛按钮
        self.pushButton_red_win.clicked.connect(self.red_win)  # 红方获胜按钮
        self.pushButton_blue_win.clicked.connect(self.blue_win)#青方获胜按钮
        self.pushButton_next_j.clicked.connect(self.ju)#下一局
        self.pushButton_next_c.clicked.connect(self.next_c)#下一场

        self.pushButton_time.clicked.connect(self.start_countdown_2)#计时按钮，休息60s
        self.pushButton_lock.clicked.connect(self.openxxi)#打开数据库界面
        self.pushButton_connect.clicked.connect(self.huju_lianjie)
        self.pushButton_blue_add.clicked.connect(self.blue_add) #蓝方加分
        self.pushButton_red_add.clicked.connect(self.red_add)  # 蓝方加分
        self.pushButton_blue_add_2.clicked.connect(self.blue_j) #警告
        self.pushButton_red_add_2.clicked.connect(self.red_j) #警告
        self.pushButton_blue_add_kou.clicked.connect(self.blue_minus)#扣分
        self.pushButton_red_add_kou.clicked.connect(self.red_minus)  # 扣分
        self.pushButton_blue_minus_2.clicked.connect(self.blue_jc)#警告减
        self.pushButton_red_minus_2.clicked.connect(self.red_jc)  # 警告减
        self.pushButton_blue_minus.clicked.connect(self.blue_jian) #蓝方减分
        self.pushButton_red_minus.clicked.connect(self.red_jian)  # 红方减分
        self.pushButton_blue_deduction_kou.clicked.connect(self.blue_kouj)#蓝方扣分减
        self.pushButton_red_deduction_kou.clicked.connect(self.red_kouj)#红方扣分减
    #红方扣分减
    def red_kouj(self):
        self.red_deduction_num -= 1
        self.label_red_deduction.setText(str(self.red_deduction_num))
        self.blue_num_2 -= 1
        self.label_blue_csore.setText(str(self.blue_num_2))
        if self.round == 1:
            self.label_red_one_2.setText(str(self.red_deduction_num))
            self.label_blue_one_1.setText(str(self.blue_num_2))
        elif self.round == 2:
            self.label_red_two_2.setText(str(self.red_deduction_num))
            self.label_blue_two_1.setText(str(self.blue_num_2))
        elif self.round == 3:
            self.label_red_three_2.setText(str(self.red_deduction_num))
            self.label_blue_three_1.setText(str(self.blue_num_2))
        elif self.round == 4:
            self.label_red_four_2.setText(str(self.red_deduction_num))
            self.label_blue_four_1.setText(str(self.blue_num_2))
    # 蓝方扣分减
    def blue_kouj(self):
        self.blue_deduction_num-=1
        self.label_blue_deduction.setText(str(self.blue_deduction_num))  # 显示扣分
        self.red_num_2 -= 1
        self.label_red_csore.setText(str(self.red_num_2))  # 对面加分
        if self.round == 1:
            self.label_blue_one_2.setText(str(self.blue_deduction_num))
            self.label_red_one_1.setText(str(self.red_num_2))
        elif self.round == 2:
            self.label_blue_two_2.setText(str(self.blue_deduction_num))
            self.label_red_two_1.setText(str(self.red_num_2))
        elif self.round == 3:
            self.label_blue_three_2.setText(str(self.blue_deduction_num))
            self.label_red_three_1.setText(str(self.red_num_2))
        elif self.round == 4:
            self.label_blue_four_2.setText(str(self.blue_deduction_num))
            self.label_red_four_1.setText(str(self.red_num_2))

    # 蓝方减分
    def blue_jian(self): #蓝方减分
        self.blue_num_2-=1
        self.label_blue_csore.setText(str(self.blue_num_2))
        if self.round == 1:
            self.label_blue_one_1.setText(str(self.blue_num_2))
        elif self.round == 2:
            self.label_blue_two_1.setText(str(self.blue_num_2))
        elif self.round == 3:
            self.label_blue_three_1.setText(str(self.blue_num_2))
        elif self.round == 4:
            self.label_blue_four_1.setText(str(self.blue_num_2))

    # 红方减分
    def red_jian(self):
        self.red_num_2 -= 1
        self.label_red_csore.setText(str(self.red_num_2))
        if self.round == 1:
            self.label_red_one_1.setText(str(self.red_num_2))
        elif self.round == 2:
            self.label_red_two_1.setText(str(self.red_num_2))
        elif self.round == 3:
            self.label_red_three_1.setText(str(self.red_num_2))
        elif self.round == 4:
            self.label_red_four_1.setText(str(self.red_num_2))
    #下一场，所有初始化，信息上传数据库
    def next_c(self):
        #蓝方获胜
        if self.win==1:
            mysq.gengai(self.label_blue_name.text(),self.blue_win_fangshi,self.label_blue_csore.text(),self.label_red_csore.text(),self.label_blue_deduction.text(),self.label_red_deduction.text(),
                        self.label_blue_one_1.text(),self.label_red_one_1.text(),self.label_blue_one_2.text(),self.label_red_one_2.text(),self.label_blue_two_1.text(),self.label_red_two_1.text(),
                        self.label_blue_two_2.text(),self.label_red_two_2.text(),self.label_blue_three_1.text(),self.label_red_three_1.text(),self.label_blue_three_2.text(),self.label_red_three_2.text(),
                        self.label_blue_four_1.text(),self.label_red_four_1.text(),self.label_blue_four_2.text(),self.label_red_four_2.text(),str(int(self.label_blue_csore.text())-int(self.label_red_csore.text())),'0',self.bisaixuhao)
            mysq.xiu(self.blue_bian,self.label_blue_site.text(),self.label_blue_name.text(),self.label_6.text()+self.label_11.text())
        # 红方获胜
        elif self.win==2:
            mysq.gengai(self.label_red_name.text(),self.red_win_fangshi,self.label_blue_csore.text(),self.label_red_csore.text(),self.label_blue_deduction.text(),self.label_red_deduction.text(),
                        self.label_blue_one_1.text(),self.label_red_one_1.text(),self.label_blue_one_2.text(),self.label_red_one_2.text(),self.label_blue_two_1.text(),self.label_red_two_1.text(),
                        self.label_blue_two_2.text(),self.label_red_two_2.text(),self.label_blue_three_1.text(),self.label_red_three_1.text(),self.label_blue_three_2.text(),self.label_red_three_2.text(),
                        self.label_blue_four_1.text(),self.label_red_four_1.text(),self.label_blue_four_2.text(),
                        self.label_red_four_2.text(),
                        '0',
                        str(int(self.label_red_csore.text())-int(self.label_blue_csore.text())),
                        self.bisaixuhao)
            mysq.xiu(self.red_bian, self.label_red_site.text(), self.label_red_name.text(),
                     self.label_6.text() + self.label_11.text())
        else:
            QMessageBox.information(self, "提示", "请选择获胜方", QMessageBox.Ok)


        self.label_blue_head_dian.setText('--')
        self.label_blue_huju_dian.setText('--')
        self.label_red_head_dian.setText('--')
        self.label_red_huju_dian.setText('--')
        self.label_dfq_dian_1.setText('--')
        self.label_dfq_dian_2.setText('--')
        self.label_blue_deduction.setText('0')
        self.label_red_deduction.setText('0')
        self.label_blue_csore.setText('0')
        self.label_red_csore.setText('0')
        self.label_blue_fs.setText('0')
        self.label_red_fs.setText('0')
        self.label_blue_fs_one_1.setText('0')
        self.label_blue_fs_one_2.setText('0')
        self.label_red_fs_one_1.setText('0')
        self.label_red_fs_one_2.setText('0')
        self.label_blue_one_1.setText('0')
        self.label_blue_one_2.setText('0')
        self.label_blue_one_3.setText('0')
        self.label_red_one_1.setText('0')
        self.label_red_one_2.setText('0')
        self.label_red_one_3.setText('0')
        self.label_blue_two_1.setText('0')
        self.label_blue_two_2.setText('0')
        self.label_blue_two_3.setText('0')
        self.label_red_two_1.setText('0')
        self.label_red_two_2.setText('0')
        self.label_red_two_3.setText('0')
        self.label_blue_three_1.setText('0')
        self.label_blue_three_2.setText('0')
        self.label_blue_three_3.setText('0')
        self.label_red_three_1.setText('0')
        self.label_red_three_2.setText('0')
        self.label_red_three_3.setText('0')
        self.label_blue_four_1.setText('0')
        self.label_blue_four_2.setText('0')
        self.label_blue_four_3.setText('0')
        self.label_red_four_1.setText('0')
        self.label_red_four_2.setText('0')
        self.label_red_four_3.setText('0')
        self.label_blue_head_fs.setText('')
        self.label_blue_huju_fs.setText('')
        self.label_blue_head_id.setText('')
        self.label_blue_huju_id.setText('')
        self.label_blue_head_lidu.setText('')
        self.label_blue_huju_lidu.setText('')
        self.label_red_head_fs.setText('')
        self.label_red_huju_fs.setText('')
        self.label_red_head_id.setText('')
        self.label_red_huju_id.setText('')
        self.label_red_head_lidu.setText('')
        self.label_red_huju_lidu.setText('')
        self.label_shebei.setText('')
        self.round=1
        self.label_bureau.setText('第{}局'.format(self.round))
        self.xuhao=0
        se= 0  # 蓝方分数
        self.blue_num_2 = 0
        self.red_num_2 = 0  # 红方分数
        self.red_num_2 = 0
        self.blue_warning_num = 0  # 蓝方警告
        self.red_warning_num = 0  # 红方警告
        self.blue_deduction_num = 0  # 蓝方扣分
        self.red_deduction_num = 0  # 红方扣分
        self.round = 1  # 第几局
        self.huju = []  # 串口传输的信息
        self.placeid = ''  # 场地号
        self.red_head_id = ''  # 红方头盔id
        self.red_huju_id = ''  # 红方护具id
        self.blue_head_id = ''  # 蓝方头盔id
        self.blue_huju_id = ''  # 蓝方护具id
        self.blue_huju_dian = ''  # 电量
        self.blue_head_dian = ''
        self.red_huju_dian = ''
        self.red_head_dian = ''
        self.blue_head_lidu = ''
        self.blue_huji_lidu = ''
        self.red_head_lidu = ''
        self.red_huji_lidu = ''
        self.id = ''

        #显示下一场比赛的信息
        self.results = mysq.execute_query()
        if self.current_row < len(self.results):
            self.current_row += 1
        else:
            QMessageBox.information(self, "提示", "已经是最后一场了", QMessageBox.Ok)
        row_data = self.results[self.current_row]
        self.label_6.setText(row_data[0])
        self.label_11.setText(row_data[1])
        self.label_12.setText(row_data[2])
        self.label_14.setText(row_data[3])
        self.label_blue_name.setText(row_data[4])
        self.label_blue_site.setText(row_data[5])
        self.label_red_name.setText(row_data[6])
        self.label_red_site.setText(row_data[7])
        self.bisaixuhao=row_data[8]
        self.blue_bian=row_data[9]
        self.red_bian=row_data[10]

    # 连接串口
    def huju_lianjie(self):
        self.huju_thread.start_huju=True
        self.compare_thread.start_label = True

    # 蓝警告减
    def blue_jc(self):#警告减
        #如果是偶数，需要把得分的减去，扣分的消去
        if  self.blue_warning_num %2==0:
            self.blue_warning_num -= 1 #警告减1
            self.blue_deduction_num -=1 #扣分减1
            self.red_num_2 -= 1  #对面得分减1
            #显示刷新
            self.label_blue_deduction.setText(str(self.blue_deduction_num))
            self.label_red_csore.setText(str(self.red_num_2))
            if self.round==1:
                self.label_blue_one_3.setText(str(self.blue_warning_num))#警告
                self.label_blue_one_2.setText(str(self.blue_deduction_num))#扣分
                self.label_red_one_1.setText(str(self.red_num_2)) #对面得分减
            elif self.round==2:
                self.label_blue_two_3.setText(str(self.blue_warning_num))
                self.label_blue_two_2.setText(str(self.blue_deduction_num))#扣分
                self.label_red_two_1.setText(str(self.red_num_2)) #对面得分减
            elif self.round==3:
                self.label_blue_three_3.setText(str(self.blue_warning_num))
                self.label_blue_three_2.setText(str(self.blue_deduction_num))#扣分
                self.label_red_three_1.setText(str(self.red_num_2)) #对面得分减
            elif self.round==4:
                self.label_blue_four_3.setText(str(self.blue_warning_num))
                self.label_blue_four_2.setText(str(self.blue_deduction_num))#扣分
                self.label_red_four_1.setText(str(self.red_num_2)) #对面得分减
        else:
            self.blue_warning_num -= 1
            if self.round==1:
                self.label_blue_one_3.setText(str(self.blue_warning_num))
            elif self.round==2:
                self.label_blue_two_3.setText(str(self.blue_warning_num))
            elif self.round==3:
                self.label_blue_three_3.setText(str(self.blue_warning_num))
            elif self.round==4:
                self.label_blue_four_3.setText(str(self.blue_warning_num))
    #红警告减
    def red_jc(self):
        if  self.red_warning_num %2==0:
            self.red_warning_num -= 1 #警告减1
            self.red_deduction_num -=1 #扣分减1
            self.blue_num_2 -= 1  #对面得分减1
            #显示刷新
            self.label_red_deduction.setText(str(self.red_deduction_num))
            self.label_blue_csore.setText(str(self.blue_num_2))
            if self.round == 1:
                self.label_red_one_3.setText(str(self.red_warning_num))
                self.label_red_one_2.setText(str(self.red_deduction_num)) #扣分
                self.label_blue_one_1.setText(str(self.blue_num_2)) #得分减
            elif self.round == 2:
                self.label_red_two_3.setText(str(self.red_warning_num))
                self.label_red_two_2.setText(str(self.red_deduction_num)) #扣分
                self.label_blue_two_1.setText(str(self.blue_num_2)) #得分减
            elif self.round == 3:
                self.label_red_three_3.setText(str(self.red_warning_num))
                self.label_red_three_2.setText(str(self.red_deduction_num)) #扣分
                self.label_blue_three_1.setText(str(self.blue_num_2)) #得分减
            elif self.round == 4:
                self.label_red_four_3.setText(str(self.red_warning_num))
                self.label_red_four_2.setText(str(self.red_deduction_num)) #扣分
                self.label_blue_four_1.setText(str(self.blue_num_2)) #得分减
        else:
            self.red_warning_num  -= 1
            if self.round==1:
                self.label_red_one_3.setText(str(self.red_warning_num))
            elif self.round==2:
                self.label_red_two_3.setText(str(self.red_warning_num))
            elif self.round==3:
                self.label_red_three_3.setText(str(self.red_warning_num))
            elif self.round==4:
                self.label_red_four_3.setText(str(self.red_warning_num))
    # 扣分
    def blue_minus(self):
        self.blue_deduction_num += 1
        self.label_blue_deduction.setText(str(self.blue_deduction_num))  #显示扣分
        self.red_num_2 +=1
        self.label_red_csore.setText(str(self.red_num_2)) #对面加分
        if self.round == 1:
            self.label_blue_one_2.setText(str(self.blue_deduction_num))
            self.label_red_one_1.setText(str(self.red_num_2))
        elif self.round == 2:
            self.label_blue_two_2.setText(str(self.blue_deduction_num))
            self.label_red_two_1.setText(str(self.red_num_2))
        elif self.round == 3:
            self.label_blue_three_2.setText(str(self.blue_deduction_num))
            self.label_red_three_1.setText(str(self.red_num_2))
        elif self.round == 4:
            self.label_blue_four_2.setText(str(self.blue_deduction_num))
            self.label_red_four_1.setText(str(self.red_num_2))

    # 扣分
    def red_minus(self):
        self.red_deduction_num += 1
        self.label_red_deduction.setText(str(self.red_deduction_num ))
        self.blue_num_2+=1
        self.label_blue_csore.setText(str(self.blue_num_2))
        if self.round == 1:
            self.label_red_one_2.setText(str(self.red_deduction_num))
            self.label_blue_one_1.setText(str(self.blue_num_2))
        elif self.round == 2:
            self.label_red_two_2.setText(str(self.red_deduction_num))
            self.label_blue_two_1.setText(str(self.blue_num_2))
        elif self.round == 3:
            self.label_red_three_2.setText(str(self.red_deduction_num))
            self.label_blue_three_1.setText(str(self.blue_num_2))
        elif self.round == 4:
            self.label_red_four_2.setText(str(self.red_deduction_num))
            self.label_blue_four_1.setText(str(self.blue_num_2))

    # 警告
    def blue_j(self):
        self.blue_warning_num += 1
        if self.blue_warning_num %2==0:  #警告为偶数
            self.blue_deduction_num+=1 #自己扣分加一
            self.label_blue_deduction.setText(str(self.blue_deduction_num))
            self.red_num_2+=1            #对面得分加一
            self.label_red_csore.setText(str(self.red_num_2))
        if self.round==1:
            self.label_blue_one_3.setText(str(self.blue_warning_num))
            self.label_blue_one_2.setText(str(self.blue_deduction_num))
            self.label_red_one_1.setText(str(self.red_num_2))
        elif self.round==2:
            self.label_blue_two_3.setText(str(self.blue_warning_num))
            self.label_blue_two_2.setText(str(self.blue_deduction_num))
            self.label_red_two_1.setText(str(self.red_num_2))
        elif self.round==3:
            self.label_blue_three_3.setText(str(self.blue_warning_num))
            self.label_blue_three_2.setText(str(self.blue_deduction_num))
            self.label_red_three_1.setText(str(self.red_num_2))
        elif self.round==4:
            self.label_blue_four_3.setText(str(self.blue_warning_num))
            self.label_blue_foyr_2.setText(str(self.blue_deduction_num))
            self.label_red_foyr_1.setText(str(self.red_num_2))

    # 警告
    def red_j(self):
        self.red_warning_num += 1
        if self.red_warning_num %2==0:  #警告为奇数
            self.red_deduction_num+=1  #自己扣分加一
            self.label_red_deduction.setText(str(self.red_deduction_num))
            self.blue_num_2+=1            #对面得分加一
            self.label_blue_csore.setText(str(self.blue_num_2))
        if self.round==1:
            self.label_red_one_3.setText(str(self.red_warning_num))
            self.label_red_one_2.setText(str(self.red_deduction_num))
            self.label_blue_one_1.setText(str(self.blue_num_2))
        elif self.round==2:
            self.label_red_two_3.setText(str(self.red_warning_num))
            self.label_red_two_2.setText(str(self.red_deduction_num))
            self.label_blue_two_1.setText(str(self.blue_num_2))
        elif self.round==3:
            self.label_red_three_3.setText(str(self.red_warning_num))
            self.label_red_three_2.setText(str(self.red_deduction_num))
            self.label_blue_three_1.setText(str(self.blue_num_2))
        elif self.round==4:
            self.label_red_four_3.setText(str(self.red_warning_num))
            self.label_red_foyr_2.setText(str(self.red_deduction_num))
            self.label_blue_foyr_1.setText(str(self.blue_num_2))

    # 加分
    def blue_add(self):
        self.blue_num_2+=1
        self.label_blue_csore.setText(str(self.blue_num_2))
        if self.round==1:
            self.label_blue_one_1.setText(str(self.blue_num_2))
        elif self.round==2:
            self.label_blue_two_1.setText(str(self.blue_num_2))
        elif self.round==3:
            self.label_blue_three_1.setText(str(self.blue_num_2))
        elif self.round==4:
            self.label_blue_four_1.setText(str(self.blue_num_2))
    #加分
    def red_add(self):#加分
        self.red_num_2+=1
        self.label_red_csore.setText(str(self.red_num_2))
        if self.round==1:
            self.label_red_one_1.setText(str(self.red_num_2))
        elif self.round==2:
            self.label_red_two_1.setText(str(self.red_num_2))
        elif self.round==3:
            self.label_red_three_1.setText(str(self.red_num_2))
        elif self.round==4:
            self.label_red_four_1.setText(str(self.red_num_2))

    # 打开信息界面
    def openxxi(self):
        self.two=zi()
        self.two.show()

    #下一局 提示是最后一局
    def ju(self):
        self.round+=1
        if self.round==5:
            QMessageBox.information(self, "提示", "最后一局", QMessageBox.Ok)
            self.round=0
        self.label_bureau.setText("第{}局".format(self.round))
        if self.round==2:
            self.label_blue_two_1.setText(self.label_blue_one_1.text())
            self.label_blue_two_2.setText(self.label_blue_one_2.text())
            self.label_blue_two_3.setText(self.label_blue_one_3.text())
            self.label_red_two_1.setText(self.label_red_one_1.text())
            self.label_red_two_2.setText(self.label_red_one_2.text())
            self.label_red_two_3.setText(self.label_red_one_3.text())
        if self.round==3:
            self.label_blue_three_1.setText(self.label_blue_two_1.text())
            self.label_blue_three_2.setText(self.label_blue_two_2.text())
            self.label_blue_three_3.setText(self.label_blue_two_3.text())
            self.label_red_three_1.setText(self.label_red_two_1.text())
            self.label_red_three_2.setText(self.label_red_two_2.text())
            self.label_red_three_3.setText(self.label_red_two_3.text())
        if self.round==4:
            self.label_blue_four_1.setText(self.label_blue_three_1.text())
            self.label_blue_four_2.setText(self.label_blue_three_2.text())
            self.label_blue_four_3.setText(self.label_blue_three_3.text())
            self.label_red_four_1.setText(self.label_red_three_1.text())
            self.label_red_four_2.setText(self.label_red_three_2.text())
            self.label_red_four_3.setText(self.label_red_three_3.text())

    # 蓝方赢界面
    def blue_win(self):
        self.one = bw()
        self.one.value_signal.connect(self.value_signal)
        self.one.show()
        self.timer_2.stop()
        self.timer.stop()
        self.win = 1
        self.huju_thread.start_huju = False
        self.compare_thread.start_label = False

    # 获取蓝方获胜方式
    def value_signal(self,value):
        self.blue_win_fangshi=value
        print(self.blue_win_fangshi)

    # 红方赢界面
    def red_win(self):
        self.three=rw()
        self.three.value_signal.connect(self.value_signal_2)
        self.three.show()
        self.timer_2.stop()
        self.timer.stop()
        self.win=2
        self.huju_thread.start_huju = False
        self.compare_thread.start_label = False

    # 红方获胜方式
    def value_signal_2(self,value):
        self.red_win_fangshi=value
        print(self.red_win_fangshi)

    def start_countdown_2(self):  # 倒计时
        self.huju_thread.start_huju=False
        self.compare_thread.start_label = False
        if self.countdown_time_2 > 0:
            self.timer_2.start(1000)  # update every second
            self.pushButton_time.setDisabled(True)  # 禁用

    def update_time_2(self):  # 倒计时
        self.countdown_time_2 -= 1
        minutes = self.countdown_time_2 // 60
        seconds = self.countdown_time_2 % 60
        time_str = f"{minutes:02d}:{seconds:02d}"
        self.label_time.setText(time_str)

        if self.countdown_time_2 <= 0:
            self.timer_2.stop()
            self.pushButton_time.setDisabled(False)
            self.countdown_time_2 = 10
            self.label_time.setText('02:00')

    def start_countdown(self):  # 休息倒计时
        if self.countdown_time > 0:
            self.timer.start(1000)  # update every second
            self.pushButton_start.setDisabled(True)  # 禁用

    def update_time(self):  # 休息倒计时
        self.countdown_time -= 1
        minutes = self.countdown_time // 60
        seconds = self.countdown_time % 60
        time_str = f"{minutes:02d}:{seconds:02d}"
        self.label_time.setText(time_str)

        if self.countdown_time <= 0:
            self.timer.stop()
            self.pushButton_start.setDisabled(False)
            self.countdown_time = 10
            self.label_time.setText('02:00')


if __name__ == '__main__':

    QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    # 适应高DPI设备
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    # 解决图片在不同分辨率显示模糊问题

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = Taekwondo1()
    MainWindow.show()
    sys.exit(app.exec_())
